package hander

import (
	"day20/api/router"
	"day20/global"
	"day20/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateLiveRoom(c *gin.Context) {
	var req router.CreateLiveRoomReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证错误",
			"data": err.Error(),
		})
		return
	}
	room := model.LiveRoom{
		NickName:   req.NickName,
		Status:     req.Status,
		OnlineNum:  req.OnlineNum,
		GiftNum:    req.GiftNum,
		CommentNum: req.CommentNum,
	}

	if err := global.DB.Create(&room).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "创建失败",
			"data": err.Error(),
		})
		return

	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "创建成功",
		"data": room.Id,
	})
	return

}

func ListLiveRoom(c *gin.Context) {
	var req router.ListLiveRoom

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证错误",
			"data": err.Error(),
		})
		return
	}
	var room []model.LiveRoom

	if err := global.DB.Find(&room).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "展示失败",
			"data": err.Error(),
		})
		return

	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": room,
	})
	return
}

func InfoLiveRoom(c *gin.Context) {
	var req router.InfoLiveRoom

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证错误",
			"data": err.Error(),
		})
		return
	}
	var room model.LiveRoom

	if err := global.DB.Where("id = ?", req.Id).First(&room).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "详情展示失败",
			"data": err.Error(),
		})
		return

	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "详情展示成功",
		"data": room,
	})
	return
}

func UpdateRoom(c *gin.Context) {
	var req router.UpdateRoom

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证错误",
			"data": err.Error(),
		})
		return
	}
	room := model.LiveRoom{
		Id:     req.Id,
		Status: req.Status,
	}

	if err := global.DB.Updates(&room).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "修改失败",
			"data": err.Error(),
		})
		return

	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": room,
	})

}
